package com.zsy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsy.dao.*;
import com.zsy.dto.OrderDto;
import com.zsy.pojo.*;
import com.zsy.service.OrderService;
import com.zsy.utils.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired(required = false)
    private OrdersDao ordersDao;

    @Autowired(required = false)
    private OrderDetailDao orderDetailDao;

    @Autowired(required = false)
    private ShoppingCartDao shoppingCartDao;

    @Autowired(required = false)
    private UserDao userDao;

    @Autowired(required = false)
    private AddressBookDao addressBookDao;

    @Override
    @Transactional
    public void submit(Order order, Long userId) {
        //查询购物车里的数据
        List<ShoppingCart> shoppingCartList = shoppingCartDao.list(userId);
        //根据当前登录userId查询对应用户
        User user = userDao.findById(userId);
        //根据地址Id，查询地址数据
        AddressBook addressBook = addressBookDao.getById(order.getAddressBookId());
        //用UUIDUtils工具类生成订单id
        long orderId = UUIDUtils.getUUIDInOrderId().longValue();
        //使用浮点数计算工具BigDecimal
        BigDecimal totalAmount = new BigDecimal("0");

        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //按下alt+enter
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(UUIDUtils.getUUIDInOrderId().longValue()); //订单项目的主键
            orderDetail.setName(shoppingCart.getName());  //购物项的菜品的名字
            orderDetail.setOrderId(orderId); //订单项所属的订单的id
            orderDetail.setDishId(shoppingCart.getDishId()); //购物项的菜品的id
            orderDetail.setSetmealId(shoppingCart.getSetmealId()); //购物项的套餐的id
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor()); //口味
            orderDetail.setNumber(shoppingCart.getNumber());  //数量
            orderDetail.setAmount(shoppingCart.getAmount()); //单价
            orderDetail.setImage(shoppingCart.getImage()); //
            //计算每一个购物项的总价
            BigDecimal amount = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber() + ""));
            totalAmount = totalAmount.add(amount);
            orderDetailList.add(orderDetail);
        }

        //组装订单数据, 批量保存订单数据
        order.setId(orderId);
        order.setNumber(String.valueOf(orderId)); //订单号
        order.setStatus(1); //状态
        order.setUserId(userId);
        order.setOrderTime(LocalDateTime.now());
        order.setCheckoutTime(LocalDateTime.now());
        order.setAmount(totalAmount);
        order.setUserName(user.getName());
        order.setPhone(addressBook.getPhone());
        order.setAddress(addressBook.getDetail());
        order.setConsignee(addressBook.getConsignee());


        //插入订单
        ordersDao.save(order);
        //批量插入订单项
        orderDetailDao.saveBatch(orderDetailList);


//        F. 删除当前用户的购物车列表数据
        shoppingCartDao.clean(userId);


    }

    @Override
    public Page<Order> page(int page, int pageSize,Long number,String beginTime,String endTime) {
        PageHelper.startPage(page, pageSize);

        List<Order> orderList = ordersDao.select(number,beginTime,endTime);

        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);


        return new Page<>(orderPageInfo.getList(), orderPageInfo.getTotal(), orderPageInfo.getSize(), orderPageInfo.getPageSize());

    }

    @Override
    public Page<OrderDto> pageUser(Integer page, Integer pageSize, Long userId) {

        PageHelper.startPage(page,pageSize);

        List<Order> orderList = ordersDao.pageUser(userId);

        List<OrderDto> collect = orderList.stream().map(order -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(order, orderDto);

            List<OrderDetail> orderDetailList = orderDetailDao.pageUser(order.getId());

            orderDto.setOrderDetails(orderDetailList);
            return orderDto;
        }).collect(Collectors.toList());

        PageInfo<OrderDto> pageInfo = new PageInfo<>(collect);


        return new Page<>(pageInfo.getList(),pageInfo.getTotal(),pageInfo.getSize(),pageInfo.getPageNum(),pageInfo.getPages());

    }

}
